package com.swpu.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.Mysqlx;
import com.swpu.commonutils.R;
import com.swpu.educms.entity.CrmBanner;
import com.swpu.educms.service.CrmBannerService;
import io.lettuce.core.Limit;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.EnumSet;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ygq
 * @since 2021-10-22
 */
@RestController
@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;


    //分页查询banner
    @GetMapping("/getBanner/{page}/{limit}")
    public R getAll(@PathVariable long page,
                    @PathVariable long limit){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        bannerService.page(bannerPage,null);
        long total = bannerPage.getTotal();
        List<CrmBanner> records = bannerPage.getRecords();
        return R.ok().data("items",records).data("total",total);
    }

    // 添加Banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok();
    }

    //删除Banner
    @DeleteMapping("/{BannerId}")
    public R Deletebanner(@PathVariable String BannerId){
        boolean id = bannerService.removeById(BannerId);
        if (id)
        return R.ok();
        else
            return R.error().message("删除失败");
    }

    //查询单个Banneer
    @GetMapping("/getOne/{id}")
    public R getOne(@PathVariable String id){
        CrmBanner byId = bannerService.getById(id);
        return R.ok().data("item",byId);
    }
    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

}

